package com.meiyu.live.ui.mine;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.meiyu.live.BaseActivity;
import com.meiyu.live.R;
import com.meiyu.live.comm.Constant;
import com.meiyu.live.net.param.NoticeParams;
import com.meiyu.live.net.response.NoticeResponse;
import com.meiyu.live.popj.Notice;
import com.meiyu.live.util.RippleUtils;
import com.meiyu.live.widget.MSGView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 消息界面
 */
public class MessageActivity extends BaseActivity implements View.OnClickListener {


    private LayoutInflater inflater;
    private MSGView msgView;
    private SwipeRefreshLayout refreshLayout;
    private ListView listView;
    private DataListAdapter adapter;
    private View btn_back, footerView;

    private ImageLoader imageLoader;
    private DisplayImageOptions options;
    private SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd hh:mm");

    private int index = 1;
    private boolean hasMore = true;
    private List<Notice> noticeList = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        init();
    }

    protected void init() {
        inflater = LayoutInflater.from(this);
        imageLoader = ImageLoader.getInstance();
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.icon_def_header)          // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.drawable.icon_def_header)  // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.icon_def_header)       // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)                          // 设置下载的图片是否缓存在SD卡中
                .build();
        msgView = (MSGView) findViewById(R.id.msgView);

        btn_back = findViewById(R.id.btn_back);
        btn_back.setOnClickListener(this);

        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData(true);
            }
        });
        listView = (ListView) findViewById(R.id.listview);

        footerView = inflater.inflate(R.layout.view_loadmore, null);
        listView.addFooterView(footerView);
        footerView.setVisibility(View.GONE);

        adapter = new DataListAdapter();
        listView.setAdapter(adapter);
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int scrollState) {
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) { // 判断滚动到底部
                    if (absListView.getLastVisiblePosition() == (absListView.getCount() - 1) && hasMore) {
                        footerView.setVisibility(View.VISIBLE);
                        getData(false);
                    }
                }
            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {

            }
        });

        getData(true);
    }

    private void getData(final boolean reload) {
        msgView.showLoading();
        //热门直播接口
        if (reload) {
            index = 1;
            hasMore = true;
        }
//        BaseBodyParams b = new BaseBodyParams(UserConfig.getInstance().getSession_token());
        NoticeParams noticeParams = new NoticeParams(0, 0, index);
        noticeParams.setUrl(Constant.UrlParams.NOTICE);
        request(noticeParams, NoticeResponse.class, new Response.Listener<NoticeResponse>() {
            @Override
            public void onResponse(NoticeResponse noticeResponse) {
                refreshLayout.setRefreshing(false);
                footerView.setVisibility(View.GONE);
                if (noticeResponse != null) {
                    if (reload) {
                        noticeList.clear();
                    }
                    noticeList.addAll(noticeResponse.getHash());
                    if (noticeResponse.getHash().size() == 0) {
                        hasMore = false;
                    }
                    adapter.notifyDataSetChanged();
                    if (index == 1 && noticeResponse.getHash().size() == 0) {
                        msgView.show_empty();
                    } else {
                        msgView.dismiss();
                        index += 1;
                    }
                } else {
                    msgView.show_error();
                }
            }
        });
    }

    @Override
    public void onError() {
        super.onError();
        refreshLayout.setRefreshing(false);
        footerView.setVisibility(View.GONE);
        msgView.show_error();
    }

    @Override
    public void onClick(View view) {
        if (view == btn_back) {
            finish();
        }
    }

    class DataListAdapter extends BaseAdapter {

        private class ViewHolder {
            View layout_root;
            TextView tv_title, tv_content, tv_time;
        }

        int mainColor = getResources().getColor(R.color.main_theme_color);

        View.OnClickListener onItemClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int index = (int) view.getTag();
                final Notice bean = noticeList.get(index);
                long id = bean.getId();
            }
        };

        @Override
        public int getCount() {
            return noticeList != null ? noticeList.size() : 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.adapter_sys_message, null);
                holder = new ViewHolder();
                holder.layout_root = convertView.findViewById(R.id.layout_root);
                RippleUtils.setRippleDrawable(holder.layout_root, mainColor);
                holder.layout_root.setOnClickListener(onItemClickListener);
                holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
                holder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
                holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.layout_root.setTag(position);
            final Notice bean = noticeList.get(position);
            holder.tv_title.setText(bean.getTitle());
            holder.tv_content.setText(bean.getContent());
            if (bean.time == null) {
                bean.time = dateFormat.format(new Date(bean.getCreate_time()));
            }
            holder.tv_time.setText(bean.time);
            return convertView;
        }
    }
}
